package web

import (
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

/*
todo
	录事故：
		事故药品++
		事故设备++
		事故耗材++
			生成事故物品关系表数据
	删事故
		删除事故信息、事故物品关系数据
	修改事故
		表单修改
*/

type AccidentHandler struct {
	svc *service.AccidentService
}

func NewAccidentHandler(svc *service.AccidentService) *AccidentHandler {
	return &AccidentHandler{
		svc: svc,
	}
}

func (a *AccidentHandler) RegistryRoutes(server *gin.Engine) {
	ag := server.Group("/accident")
	ag.POST("/create", a.Create)
	ag.POST("/query", a.Query)
	ag.POST("/delete", a.Delete)
	ag.POST("/related-item", a.QueryRelatedItem)
	ag.POST("/update-related-item", a.UpdateRelatedItem)
	ag.POST("/item-all", a.QueryItemAll)
	//ag.POST("/tree-item-related", a.QueryTreeItemRelated)
}

func (a *AccidentHandler) Create(ctx *gin.Context) {
	type QueryReq struct {
		Title         string   `json:"title"`
		TitleEn       string   `json:"title_en"`
		Year          string   `json:"year"`
		Location      string   `json:"location"`
		Dead          int      `json:"dead"`
		Injure        int      `json:"injure"`
		Degree        int      `json:"degree"`
		Season        string   `json:"season"`
		Chemicals     []string `json:"chemicals"`
		Equipments    []string `json:"equipments"`
		Consumables   []string `json:"consumables"`
		Description   string   `json:"description"`
		DescriptionEn string   `json:"description_en"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	createdRows, err := a.svc.Create(ctx, domain.Accident{
		Title:         req.Title,
		TitleEn:       req.TitleEn,
		Year:          req.Year,
		Location:      req.Location,
		Dead:          req.Dead,
		Injure:        req.Injure,
		Degree:        req.Degree,
		Season:        req.Season,
		Chemicals:     req.Chemicals,
		Equipments:    req.Equipments,
		Consumables:   req.Consumables,
		Description:   req.Description,
		DescriptionEn: req.DescriptionEn,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Rows": createdRows,
	})
}

func (a *AccidentHandler) Query(ctx *gin.Context) {
	type QueryReq struct {
		Title        string `json:"title"`
		ChemicalId   string `json:"chemical_id"`
		EquipmentId  string `json:"equipment_id"`
		ConsumableId string `json:"consumable_id"`
		Season       string `json:"season"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	accident, err := a.svc.Query(ctx, domain.AccidentSearchParam{
		Title:        req.Title,
		ChemicalId:   req.ChemicalId,
		EquipmentId:  req.EquipmentId,
		ConsumableId: req.ConsumableId,
		Season:       req.Season,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, accident)
}

func (a *AccidentHandler) Delete(ctx *gin.Context) {
	type QueryReq struct {
		Ids []string `json:"ids"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	affectedRows, err := a.svc.Delete(ctx, req.Ids)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"rows": affectedRows,
	})
}

func (a *AccidentHandler) QueryRelatedItem(ctx *gin.Context) {
	type QueryReq struct {
		AccidentId string `json:"accident_id"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	accident, err := a.svc.QueryRelatedItem(ctx, req.AccidentId)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, accident)
}

func (a *AccidentHandler) UpdateRelatedItem(ctx *gin.Context) {
	type QueryReq struct {
		AccidentId string   `json:"accident_id"`
		ItemType   string   `json:"item_type"`
		Items      []string `json:"items"`
	}

	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	rowsAffected, err := a.svc.UpdateRelatedItem(ctx, req.AccidentId, req.ItemType, req.Items)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, rowsAffected)
}

func (a *AccidentHandler) QueryItemAll(ctx *gin.Context) {
	type QueryReq struct {
		ItemType   string `json:"item_type"`
		AccidentId string `json:"accident_id"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	treeItems, err := a.svc.QueryItemAll(ctx, req.AccidentId, req.ItemType)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, treeItems)
}
